package com.monkey_pig.memoryalbum.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.bigkoo.pickerview.TimePickerView;
import com.monkey_pig.memoryalbum.R;
import com.monkey_pig.memoryalbum.common.AppContext;
import com.monkey_pig.memoryalbum.utils.BaseActivity;
import com.monkey_pig.memoryalbum.utils.L;
import com.monkey_pig.memoryalbum.utils.SPUtils;
import com.monkey_pig.memoryalbum.utils.T;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

//这个页面用于设置用户的个人信息
public class UserInfoActivity extends BaseActivity {


    private String birthday;

    @Bind(R.id.choose_birthday)
    TextView chooseBirthday;
    @Bind(R.id.btn_confirm)
    Button btnConfirm;

    private TimePickerView pvTime;

    @OnClick(R.id.choose_birthday)
    public void popTPview(View view) {
        pvTime.show();
    }
    @OnClick(R.id.btn_confirm)
    public void confirmInfo() {
//        检查用户是否选择了生日
        if (birthday==null) {
            T.showShort(AppContext.getContext(),"请先选择您的生日！");
        } else {
            //储存用户生日
            SPUtils.put(AppContext.getContext(), "Birthday", birthday);
            //将isFirstRun置否
            SPUtils.put(AppContext.getContext(), "isFirstRun", false);
            //跳转到首页
            startActivity(new Intent(UserInfoActivity.this,MainActivity.class));
        }

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_info);
        ButterKnife.bind(this);

        //初始化时间选择器
        initTimePicker();

    }


    protected void initTimePicker() {
        //时间选择器
        pvTime = new TimePickerView(this, TimePickerView.Type.YEAR_MONTH_DAY);
        //控制时间范围
        Calendar calendar = Calendar.getInstance();
        pvTime.setRange(calendar.get(Calendar.YEAR) - 100, calendar.get(Calendar.YEAR));
        pvTime.setTime(new Date());
        pvTime.setCyclic(false);
        pvTime.setCancelable(true);
        //时间选择后回调
        pvTime.setOnTimeSelectListener(new TimePickerView.OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date) {
                //修改TextView中的内容
                chooseBirthday.setText(getTime(date));
                //将生日信息保存本地
                birthday = getTime(date);

            }
        });

    }

    public static String getTime(Date date) {
        //格式化时间
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(date);
    }

}
